package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.OrderSettingQD;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 预约设置
 */
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private OrderSettingService orderSettingService;

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * Excel文件上传,并解析文件内容保存到数据库
     * @param excelFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile  excelFile){
        log.info("文件上传:{}",excelFile);

        //原始文件名
        String originalFilename = excelFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String objectName = UUID.randomUUID().toString() + extension;

        try {
            String filePath = aliOssUtil.upload(excelFile.getBytes(),objectName);

            //创建一个输入流
            InputStream inputStream = excelFile.getInputStream();
            //通过输入流读取指定Excel文件
            XSSFWorkbook excel = new XSSFWorkbook(inputStream);
            //读取Excel文件中的第一个sheet
            XSSFSheet sheet = excel.getSheetAt(0);

            //获取Sheet页中的最后一行的行号
            int lastRowNum = sheet.getLastRowNum();

            for (int i = 1; i < lastRowNum; i++) {
                //获取sheet页中行
                XSSFRow titleRow = sheet.getRow(i);
                //获取行的第一个单元格
                XSSFCell cell1 = titleRow.getCell(0);
                //获取单元格中的文本内容
                Date cellValue1 = cell1.getDateCellValue();
                //获取行的第二个单元格
                XSSFCell cell2 = titleRow.getCell(1);
                //获取单元格的文本内容
                Double cellValue2 = cell2.getNumericCellValue();

                System.out.println(cellValue1 + ""+ cellValue2);

                int calllValue = cellValue2.intValue();
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(cellValue1);
                orderSetting.setNumber(calllValue);

                orderSettingDao.inster(orderSetting);

            }
            //关闭资源
            inputStream.close();
            excel.close();

            return new Result(true,MessageConst.UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
            log.error("文件上传失败");
        }
        return new Result(false,MessageConst.ACTION_FAIL);
    }

    /**
     * 通过月份获取预约
     * @param year
     * @param month
     * @return
     */
    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(Integer year,Integer month){

        List<OrderSettingQD> orderSettingQDS = orderSettingService.getOrderSettingByMonth(year,month);

        return new Result(true,MessageConst.ORDERSETTING_SUCCESS,orderSettingQDS);
    }

    /**
     * 通过 "日期" 编辑每天对应的可预约人数
     * @param orderSetting
     * @return
     */
    @PostMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS,orderSetting);

    }
}
